package com.wenhui.schoolyardinfo.mine.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.QMUITopBar;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.base.BaseActivity;
import com.wenhui.schoolyardinfo.mine.adapter.AlbumPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/7/12 11:25
 * introduce :
 */

public class ShowPicturesActivity extends BaseActivity {

    private static final String TAG = "ShowPicturesActivity";
    @BindView(R.id.imv_back)
    ImageView imvBack;
    @BindView(R.id.layout_back)
    RelativeLayout layoutBack;
    @BindView(R.id.tev_title)
    TextView tevTitle;
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.picture_view_pager)
    ViewPager pictureViewPager;

    private List<String> photoUrlList;
    private int currentPosition;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_pictures);
        ButterKnife.bind(this);

        initData();
        initViews();
    }

    @Override
    protected void initData() {
        photoUrlList = new ArrayList<>();
        currentPosition = -1;
        if (getIntent().getExtras().getStringArrayList("photoUrlList") != null &&
                getIntent().getExtras().getStringArrayList("photoUrlList").size() > 0) {
            photoUrlList = getIntent().getExtras().getStringArrayList("photoUrlList");
        }
        if (getIntent().getExtras().getInt("photoUrlList") > -1) {
            currentPosition = getIntent().getExtras().getInt("currentPosition");
        }
    }

    @Override
    protected void initViews() {
        setTopBar(topbar);

        initViewPager();
    }

    private void initViewPager() {
        AlbumPagerAdapter albumPagerAdapter = new AlbumPagerAdapter(this);
        albumPagerAdapter.clearData();
        albumPagerAdapter.addAllData(photoUrlList);
        pictureViewPager.setAdapter(albumPagerAdapter);

        if (currentPosition > -1) {
            tevTitle.setText(currentPosition + 1 + " / " + photoUrlList.size());
            pictureViewPager.setCurrentItem(currentPosition);
        }

        pictureViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tevTitle.setText(position + 1 + " / " + photoUrlList.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.translate_in, R.anim.translate_out);
    }

    @OnClick(R.id.layout_back)
    public void onViewClicked() {
        finish();
    }
}
